const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstId: 0, //一级分类ID
    secId: 0, //二级分类ID
    brandId: 0, // 品牌商ID
    firstclassifyList: [], // 一级分类
    secclassifyList: [], // 二级分类
    brandList: [], //品牌商列表
    resultBrandList: [], //品牌商列表
    products: [], //商品列表
    searchBrand: '', //品牌商搜索
    showSearch: false, //显示搜索内容
    oneLeftNum: 0,
    brandLeftNum: 0,
    userScore: 0, //用户积分余额
    isShow: false
  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag,
      val = e.detail.value,
      resultBrandList = [],
      brandList = this.data.brandList;
    console.log(resultBrandList);
    resultBrandList = brandList.filter(item => {
      return item.brandName.includes(val)
    })


    this.setData({
      [str]: val,
      resultBrandList
    })

    console.log(this.data);
  },

  // 搜索框获取焦点
  searchHandle(e) {
    console.log(e);
    var flag = this.data.showSearch
    this.setData({
      showSearch: !flag
    })
  },
  // 选择一级分类
  selectHandle(e) {
    var obj = e.currentTarget.dataset.obj,
      flag = e.currentTarget.dataset.flag;
    if (flag == 'first') {
      this.setData({
        firstId: obj.productCategoryId,
        // oneLeftNum: 0,
      })
    } else if (flag == 'sec') {
      this.setData({
        secId: obj.productCategoryId
      })
    } else if (flag == 'brind') {
      this.setData({
        brandId: obj.brandId,
        // brandLeftNum: 0
      })
    } else if (flag == 'searchBrind') {
      this.setData({
        brandId: obj.brandId,
        showSearch: false
      })
    } else if (flag == 'allBrind') {
      this.setData({
        brandId: 0
      })
    }
    this.getList(false)
  },


  // 获取数据
  getList(flag, id) {
    console.log(flag);
    console.log(id);
    var url = app.globalData.url + "ScCategory/listCategory",
      that = this,
      firstId = id != 0 && flag ? id : this.data.firstId,
      secId = this.data.secId,
      brandId = this.data.brandId,
      firstclassifyList = this.data.firstclassifyList,
      secclassifyList = this.data.secclassifyList,
      brandList = this.data.brandList,
      resultBrandList = this.data.resultBrandList,
      products = this.data.products;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        firstId,
        secId,
        brandId,
      }, res => {
        console.log(res);
        if (res.flag) {
          secclassifyList = res.data.secCategories
          products = res.data.products
          console.log(flag);
          if (flag) {
            wx.removeStorageSync('cateId')
            firstclassifyList = res.data.firstCategories
            firstId = (id != 0 ? id : firstclassifyList[0].productCategoryId)
            brandList = res.data.brandList
            resultBrandList = res.data.brandList
            secId = res.data.secCategories[0].productCategoryId
          }
          wx.hideLoading()
          that.setData({
            firstclassifyList,
            secclassifyList,
            brandList,
            products,
            firstId,
            secId,
            resultBrandList
          })
          wx.stopPullDownRefresh()
        }
      },
      err => {
        console.log(err);
      })


  },

  toDetail(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '/mainPackage/pages/productDetail/productDetail?id=' + id,
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);


    this.setData({
      userScore: app.globalData.userScore,
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var id = 0;
    if (wx.getStorageSync('cateId')) {
      var id = wx.getStorageSync('cateId')
      console.log(id);
    }
    console.log(id);
    this.getList(true, id || 0)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

    this.getList(false)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})